package com.atguigu.demo.service.impl;

import com.atguigu.demo.event.ProductOnSaleEvent;
import com.atguigu.demo.service.api.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Override
    public void onSale(Integer productId) {
        System.out.println("产品上架：" + productId);
    }

    @Override
    public void putIntoCache(Integer productId) {
        System.out.println("产品进入缓存：" + productId);
    }

    @Override
    public void putIntoSearch(Integer productId) {
        System.out.println("产品进入搜索引擎：" + productId);
    }

    @Override
    public void putIntoAdQueue(Integer productId) {
        System.out.println("产品进入广告队列：" + productId);
    }

    @Override
    public void pushToGuest(Integer productId) {
        System.out.println("产品推送潜在客户：" + productId);
    }

    @Override
    @Async
    @EventListener
    public void putIntoCacheListener(ProductOnSaleEvent event) {
        log.debug("[事件监听响应][加入缓存]" + event.getProductId() + " threadName = " + Thread.currentThread().getName());
    }

    @Override
    @Async
    @EventListener
    public void putIntoSearchListener(ProductOnSaleEvent event) {
        log.debug("[事件监听响应][加入搜索引擎]" + event.getProductId() + " threadName = " + Thread.currentThread().getName());
    }

    @Override
    @Async
    @EventListener
    public void putIntoAdQueueListener(ProductOnSaleEvent event) {
        // log.info("" + (10 / 0));
        log.debug("[事件监听响应][加入广告队列]" + event.getProductId() + " threadName = " + Thread.currentThread().getName());
    }

    @Override
    @Async
    @EventListener
    public void pushToGuestListener(ProductOnSaleEvent event) {
        log.debug("[事件监听响应][客户端推送]" + event.getProductId() + " threadName = " + Thread.currentThread().getName());
    }
}